<?php
/**
 * Created by PhpStorm.
 * User: chl
 * Date: 2017/3/20 0020
 * Time: 17:02
 */
namespace app\admin\controller;
use app\models\Bases;
use think\Request;
use app\admin\validate;
use think\Db;
class Advert extends Base{
    /**广告列表
     * @return \think\response\View
     */
    public function advertList(){
        $result['list']= Db::name('advert')->order('advert_id desc')->paginate(10);
        return view('advertList',['result'=>$result]);
    }

    public function advertAdd(){
        $advertModel = new Bases('advert');
        try{
            if(Request()->isPost()){
                $post = input('param.');
                if(!empty($post['link_address'])){
                    $link_address1 = mb_substr(trim($post['link_address']),0,7);
                    $link_address2 = mb_substr(trim($post['link_address']),0,8);
                    if(strpos($link_address1,'http://') === false && strpos($link_address2,'https://') === false){
                        throw new \LogicException('链接地址必须以http://或者https://开头',10000);
                    }
                }
                $validate = $this->validate($post,'Advert.advertAdd');
                if($validate !== true){
                    throw new \LogicException($validate,1000);
                }
                unset($post['upload']);
                // 获取表单上传文件 例如上传了001.jpg
                $file = request()->file('image');
                $info = $file
                    ->validate(['size'=>20000000000,'ext'=>'jpg,png,gif,pdf'])
                    ->rule('uniqid')
                    ->move(ROOT_PATH . 'public' . DS . 'uploads/advert');

                if(!$info){
                    throw new \LogicException($file->getError(),10056);
                }
                $post['image'] = $info->getFilename();
                $rs = $advertModel->insertData($post);
                if(!$rs){
                    throw new \LogicException('操作失败',406);
                }
                return self::formatSuccessResult();
            }
        }catch(\Exception $e){
            return self::formatResult($e->getCode(),$e->getMessage());
        }
        return view('advertAdd');
    }

    public function advertSave(){
        $advertId = intval(input('advert_id'));
        $advertModel = new Bases('advert');
        try{
            if(Request()->isPost()){
                $post = input('param.');
                if(!empty($post['link_address'])){
                    $link_address1 = mb_substr(trim($post['link_address']),0,7);
                    $link_address2 = mb_substr(trim($post['link_address']),0,8);
                    if(strpos($link_address1,'http://') === false && strpos($link_address2,'https://') === false){
                        throw new \LogicException('链接地址必须以http://或者https://开头',10000);
                    }
                }
                $advertId = $post['advert_id'];
                $img = $advertModel->find("advert_id =".$advertId,'image');
                if($post['upload'] != $img['image']){
                    $path = 'https://'.$_SERVER['HTTP_HOST'].'/uploads/advert/';
                    @removeImg($path,$img['image']);//删除原先的图片
                    $file = request()->file('image');
                    if($file){
                        // 移动到框架应用根目录/public/uploads/advert 目录下
                        $info = $file->validate(['size'=>200000,'ext'=>'jpg,png,gif'])->rule('uniqid')->move(ROOT_PATH . 'public' . DS . 'uploads/advert');
                        if($info){
                            // 成功上传后 获取上传信息
                            $params['image'] = $info->getFilename();
                        }else{
                            // 上传失败获取错误信息
                            throw new \LogicException($file->getError(),10000);
                        }
                    }
                }
                $params['advert_id'] = $post['advert_id'];
                $params['title'] = $post['title'];
                $params['link_address'] = $post['link_address'];
                $params['status'] = $post['status'];
                // 获取表单上传文件 例如上传了001.jpg
                $rs = $advertModel->updateData($params);

                if($rs === false){
                    return self::formatResult(406,'操作失败');
                }
                return self::formatSuccessResult();

            }
        }catch(\Exception $e){
            return self::formatResult($e->getCode(),$e->getMessage());
        }
        $advertOne = $advertModel->find("advert_id =".$advertId);
        return view('advertSave',['result'=>$advertOne]);
    }

    public function advertDelete(){
        $advertId = intval(input('advert_id'));
        $advertModel = new Bases('advert');
        $advertColumnModel = new Bases('advert_column');
        $rs = $advertModel->deleteDaPtaByPrimaryKey($advertId);
        $rso = $advertColumnModel->deleteByWhere('advert_id = '.$advertId);
        if($rs !== false && $rso !== false ){
            return self::formatSuccessResult();
        }return self::formatResult(406,'操作失败');
    }
}